package com.vhly.bpplat.twitter.model;

import org.kxml2.kdom.Element;

/**
 * Created by IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 2010-3-3
 * Time: 17:43:46
 */
public class User implements Model {
    public String id;
    public String name;
    public String screen_name;
    public String location;
    public String description;
    public String image_url;
    public String url;
    public boolean protect;
    public int followers_count;
    public int bgcolor;
    public int text_color;
    public int link_color;
    public int sidebar_fill_bar;
    public int sidebar_border_color;
    public int friends_count;
    public String created_at;
    public int favourites_count;
    public String background_image_url;
    public int statuses_count;
    public boolean verified;
    public boolean following;
    public Status lastStatus;

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("User: [");
        sb.append(name).append(" id:").append(id).append(" location: ").append(location);
        sb.append(']');
        return sb.toString();
    }

    public void parse(Element el) {
        Element eid = el.getElement("", "id");
        StringBuffer sb = new StringBuffer();
        if (eid != null) {
            id = (String) eid.getChild(0);
        }
        eid = el.getElement("", "name");
        if (eid != null) {
            int childCount = eid.getChildCount();
            sb.setLength(0);
            for (int i = 0; i < childCount; i++) {
                Object obj = eid.getChild(i);
                if (obj instanceof String) {
                    sb.append((String) obj);
                }
            }
            name = sb.toString();
        }
        eid = el.getElement("", "screen_name");
        if (eid != null) {
            int childCount = eid.getChildCount();
            sb.setLength(0);
            for (int i = 0; i < childCount; i++) {
                Object obj = eid.getChild(i);
                if (obj instanceof String) {
                    sb.append((String) obj);
                }
            }
            screen_name = sb.toString();
        }
        eid = el.getElement("", "location");
        if (eid != null) {
            int childCount = eid.getChildCount();
            sb.setLength(0);
            for (int i = 0; i < childCount; i++) {
                Object obj = eid.getChild(i);
                if (obj instanceof String) {
                    sb.append((String) obj);
                }
            }
            location = sb.toString();
        }
        eid = el.getElement("", "description");
        if (eid != null) {

            int childCount = eid.getChildCount();
            sb.setLength(0);
            for (int i = 0; i < childCount; i++) {
                Object obj = eid.getChild(i);
                if (obj instanceof String) {
                    sb.append((String) obj);
                }
            }
            description = sb.toString();
        }
        eid = el.getElement("", "profile_image_url");
        if (eid != null) {
            image_url = (String) eid.getChild(0);
        }
        eid = el.getElement("", "url");
        if (eid != null) {
            url = (String) eid.getChild(0);
        }
        eid = el.getElement("", "protected");
        String str;
        if (eid != null) {
            str = (String) eid.getChild(0);
            protect = str.equals("true");
        }
        eid = el.getElement("", "followers_count");
        if (eid != null) {
            str = (String) eid.getChild(0);
            followers_count = Integer.parseInt(str);
        }
        eid = el.getElement("", "profile_background_color");
        if (eid != null) {
            str = (String) eid.getChild(0);
            bgcolor = Integer.parseInt(str, 16);
        }
        eid = el.getElement("", "profile_text_color");
        if (eid != null) {
            str = (String) eid.getChild(0);
            text_color = Integer.parseInt(str, 16);
        }
        eid = el.getElement("", "profile_link_color");
        if (eid != null) {
            str = (String) eid.getChild(0);
            link_color = Integer.parseInt(str, 16);
        }
        eid = el.getElement("", "profile_sidebar_fill_color");
        if (eid != null) {
            str = (String) eid.getChild(0);
            sidebar_fill_bar = Integer.parseInt(str, 16);
        }
        eid = el.getElement("", "profile_sidebar_border_color");
        if (eid != null) {
            str = (String) eid.getChild(0);
            sidebar_border_color = Integer.parseInt(str, 16);
        }
        eid = el.getElement("", "friends_count");
        if (eid != null) {
            str = (String) eid.getChild(0);
            friends_count = Integer.parseInt(str, 16);
        }
        eid = el.getElement("", "created_at");
        if (eid != null) {
            created_at = (String) eid.getChild(0);
        }
        eid = el.getElement("", "favourites_count");
        if (eid != null) {
            str = (String) eid.getChild(0);
            favourites_count = Integer.parseInt(str, 16);
        }
        eid = el.getElement("", "profile_background_image_url");
        if (eid != null) {
            background_image_url = (String) eid.getChild(0);
        }
        eid = el.getElement("", "statuses_count");
        if (eid != null) {
            str = (String) eid.getChild(0);
            statuses_count = Integer.parseInt(str, 16);
        }
        eid = el.getElement("", "verified");
        if (eid != null) {
            str = (String) eid.getChild(0);
            verified = str.equals("true");
        }

        eid = el.getElement("", "following");
        if (eid != null) {
            str = (String) eid.getChild(0);
            following = str.equals("true");
        }

        eid = el.getElement("", "status");
        if (eid != null) {
            lastStatus = new Status();
            lastStatus.parse(eid);
        }
    }
}
